package com.example.musicplayer.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import com.example.musicplayer.listener.MusicPlayListener;
import com.example.musicplayer.playtask.MusicPlayTask;

import java.io.IOException;


/**
 * @Author: liuzh0705
 * @Date:2021/07/29/下午12:15
 * @Description: 进行后台播放
 */
public class MusicService extends Service {

    private MusicPlayTask musicPlayTask;

    private String musicPath;

    private static MediaPlayer mediaPlayer = new MediaPlayer();

    public MediaPlayer getMediaPlayer() {
        return mediaPlayer;
    }

    private MusicPlayListener listener = new MusicPlayListener() {
        @Override
        public void onMusicProgress(int progress) {

        }

        @Override
        public void onUpMusic() {

        }

        @Override
        public void onNextMusic() {

        }

        @Override
        public void onStartMusic() {

        }

        @Override
        public void onStopMusic() {

        }
    };

    private MusicPlayBinder mBinder = new MusicPlayBinder();

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    public class MusicPlayBinder extends Binder {
        /**
         * 播放音乐
         * @param musicPlyaPath
         */
        public void startMusic(String musicPlyaPath) {
            if (musicPlayTask == null) {
                // 如果第一次打开APP
                musicPath = musicPlyaPath;
                musicPlayTask = new MusicPlayTask(listener, mediaPlayer);
                // 线程执行
                musicPlayTask.execute(musicPath);
                // 前台服务 -- 通知
            } else {
                musicPath = musicPlyaPath;
                musicPlayTask = new MusicPlayTask(listener, mediaPlayer);
                // 线程执行
                musicPlayTask.execute(musicPath);
            }
        }


    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
